//
//  BkFacePublishViewController.m
//  TuDianEducation
//
//  Created by lixinkeji on 2021/1/11.
//  Copyright © 2021 zhangbenchao. All rights reserved.
//


#import "BkFacePublishViewController.h"
#import "MediaCollectionView.h"
#import "RegularExpression.h"
#import "LXSaveUserInforTool.h"
#import "PopoverViewController.h"
#import "MediaModel.h"

@interface BkFacePublishViewController ()<UITextViewDelegate,UIPopoverPresentationControllerDelegate>
@property (nonatomic, strong) UIView *textViewBgView;
@property (nonatomic, strong) UIView *septorLine;
@property (nonatomic, strong) UITextView *textView;

@property (nonatomic, strong) UILabel *placeHolderLabel;
@property (nonatomic, strong) UIButton *commitBtn;


@property (nonatomic,strong) UITextField *telTextFiled;
@property (nonatomic,strong) MediaCollectionView *mediaView;

@property (nonatomic,strong) UILabel *chirdrenLabel;
@property (nonatomic,strong) UIButton *ageBtn;

@property (nonatomic,strong) UIView *headerView;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSString *currentId;
@property (nonatomic,strong) NSArray *dataList;
@end

@implementation BkFacePublishViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"发布心得";
    [self.view addSubview:self.tableView];
    self.view.backgroundColor = kWhiteColor;
    [self requstgetsensitivewords];
}
-(UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, TSScreenWidth, TSScaleW(200))];
        [_headerView addSubview:self.textViewBgView];
        [self.textViewBgView addSubview:self.septorLine];
        [self.textViewBgView addSubview:self.textView];
        [self.textViewBgView addSubview:self.placeHolderLabel];
        self.textViewBgView.height = TSScaleW(163);
        [_headerView addSubview:self.mediaView];
        [_headerView addSubview:self.chirdrenLabel];
        [_headerView addSubview:self.ageBtn];
        [_headerView addSubview:self.commitBtn];
        _headerView.height = self.commitBtn.bottom + TSScaleW(20);
    }
    return _headerView;
}
-(UIView *)textViewBgView{
    if (!_textViewBgView) {
        _textViewBgView = [[UIView alloc]initWithFrame:CGRectMake(TSScaleW(0), TSScaleW(10), TSScreenWidth, TSScaleW(250))];
        _textViewBgView.backgroundColor = kMainBgColor;
        [_textViewBgView setCornerRadius:TSScaleW(5)];
    }
    return _textViewBgView;
}
-(UIView *)septorLine{
    if (!_septorLine) {
        _septorLine = [[UIView alloc]initWithFrame:CGRectMake(0, TSScaleW(214), _textViewBgView.width, 1.f)];
        _septorLine.backgroundColor = kMainLineColor;
    }
    return _septorLine;
}
-(UITextView *)textView
{
    if (!_textView) {
        _textView = [[UITextView alloc]initWithFrame:CGRectMake(TSScaleW(10), TSScaleW(10), _textViewBgView.width - TSScaleW(20), TSScaleW(177))];
        _textView.delegate = self;
        _textView.font = kFont(15);
        _textView.backgroundColor = kMainBgColor;
    }
    return _textView;
}
-(UILabel *)placeHolderLabel{
    if (!_placeHolderLabel) {
        _placeHolderLabel = [TSWLTools allocLabel:@"#孩子如何教育#" font:systemFont(14) textColor:kGrayTxtColor frame:CGRectMake(_textView.left, _textView.top + TSScaleW(5), _textView.width, TSScaleW(14)) textAlignment:(NSTextAlignmentLeft)];
    }
    return _placeHolderLabel;
}


-(MediaCollectionView *)mediaView{
    if (!_mediaView) {
        _mediaView = [[MediaCollectionView alloc]initWithFrame:CGRectMake(TSScaleW(15),  _textViewBgView.bottom + TSScaleW(20), TSScaleW(320), TSScaleW(50))];
        WS(weakSelf);
        _mediaView.limitCount = 5;
        _mediaView.row = 5;
        _mediaView.collectionView.backgroundColor = kWhiteColor;
        [LXSaveUserInforTool sharedUserTool].medaiType = 2;
        _mediaView.showCurrentIndexBlock = ^(NSInteger currentIndex) {
            //[weakSelf selectedIndex:currentIndex];
        };
        _mediaView.addCurrentBlock = ^{
           
        };
    }
    return _mediaView;
}
-(NSArray *)urlsArray{
    NSArray *arr = @[];
    if (!_mediaView.dataSource.count) {
        arr = @[];
    }else{
        NSMutableArray *array = [NSMutableArray array];
        for (MediaModel *model in _mediaView.dataSource) {
            if (model.imgUrl.length) {
                NSMutableString *string = [[NSMutableString alloc]initWithString:model.imgUrl];
                [array addObject:string];
            }
        }
        arr = array;
    }
    return arr;
}
-(UILabel *)chirdrenLabel{
    if (!_chirdrenLabel) {
        _chirdrenLabel = [TSWLTools allocLabel:@"   选择孩子年龄段" font:systemFont(TSScaleW(13)) textColor:kGreenColor frame:CGRectMake(0, TSScaleW(40) + _mediaView.bottom, TSScreenWidth, TSScaleW(40)) textAlignment:(NSTextAlignmentLeft)];
        _chirdrenLabel.backgroundColor = UIColorFromRGB(0xF2FFF9);
    }
    return _chirdrenLabel;
}
-(UIButton *)ageBtn{
    if (!_ageBtn) {
        
        NSArray *array = [LXSaveUserInforTool sharedUserTool].classify1Array;
        NSDictionary *dic = array.firstObject;
        _currentId = dic[@"fid"];
        _ageBtn = [TSWLTools allocButtonTitle: dic[@"name"] font:systemFont(TSScaleW(12)) textColor:kSubTxtColor image:[UIImage imageNamed:@"下拉"] frame:CGRectMake(TSScaleW(11), TSScaleW(15) + _chirdrenLabel.bottom, TSScaleW(78), TSScaleW(22)) sel:@selector(ageAction:) taget:self];
        _ageBtn.cornerRadius = _ageBtn.height/2.f;
        [_ageBtn setBorderWithWidth:.5 andColor:kMainLineColor];
        [_ageBtn setBtnLeftLabelRightImgOffSet:TSScaleW(5)];
    }
    return _ageBtn;
}
-(void)ageAction:(UIButton *)sender
{
    NSArray *array = [LXSaveUserInforTool sharedUserTool].classify1Array;
    NSMutableArray *nameArray = [NSMutableArray array];
    for (int i = 0; i < array.count; i ++) {
        NSDictionary *dic = array[i];
        [nameArray addObject:dic[@"name"]];
        
    }
    
    PopoverViewController *vc =  [self getPopVCWithData:nameArray soureView:sender];
       vc.preferredContentSize = CGSizeMake(140, 176);
      [self presentViewController:vc animated:YES completion:nil];
    WS(weakSelf);
       vc.popActionBlock = ^(NSInteger index) {
           
           NSDictionary *dic = array[index];
           [weakSelf.ageBtn setTitle:dic[@"name"] forState:(UIControlStateNormal)];
           weakSelf.currentId = dic[@"fid"];
       };
}
- (PopoverViewController *)getPopVCWithData:(NSArray *)array soureView:(UIView *)soureView{
    
    PopoverViewController *popContentVC = [PopoverViewController new];
    popContentVC.preferredContentSize = CGSizeMake(140, 132);
    popContentVC.modalPresentationStyle = UIModalPresentationPopover;
    popContentVC.dataArray= array.mutableCopy;
    UIPopoverPresentationController *popController = [popContentVC popoverPresentationController];
    popController.backgroundColor = [UIColor whiteColor];
    popController.delegate = self;
    popController.sourceView = soureView;
    popController.sourceRect = CGRectMake(20, 0, soureView.width , soureView.height);
    return popContentVC;
}
//popview 代理
- (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller {
    return UIModalPresentationNone;
}
-(UIButton *)commitBtn{
    if (!_commitBtn) {
        _commitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _commitBtn.frame = CGRectMake(TSScaleW(10), TSScaleW(50) + _ageBtn.bottom, TSScreenWidth - TSScaleW(20), kCommitBtnHeight);
        _commitBtn.backgroundColor = kRedColor;
        [_commitBtn btn:_commitBtn font:TSScaleW(16) textColor:kWhiteColor text:@"确定" image:nil sel:@selector(commitAction:) taget:self];
        [_commitBtn setCornerRadius:kCommitBtnRadius];
    }
    return _commitBtn;
}

#pragma mark ------textViewDelegate

-(void)textViewDidBeginEditing:(UITextView *)textView
{
    self.placeHolderLabel.hidden = YES;
}


- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSString *contentString = [textView.text stringByReplacingCharactersInRange:range withString:text];
    
    if (contentString.length == 0) {
        self.placeHolderLabel.hidden = NO;
    }else{
        self.placeHolderLabel.hidden = YES;
    }
   
    
    return YES;
}
-(void)commitAction:(UIButton *)sender{
    if ([self pamasIsOkOrNot]) {
        NSDictionary *pamas = @{@"content":_textView.text,@"fid":_currentId?:@"",@"images":[self urlsArray]};
        WS(weakSelf);
           [NetWorkTools postConJSONWithUrl:@"addmydynamic" parameters:pamas success:^(id responseObject) {
               
               NSString *result = responseObject[@"result"];
               NSString *resultNote = responseObject[@"resultNote"];
               if ([result integerValue] == 0) {
                   [weakSelf.navigationController popViewControllerAnimated:YES];
               }
               [MBProgressHUD showError:resultNote];
              } fail:^(NSError *error) {
                  
        }];
    }
   
}

-(BOOL)pamasIsOkOrNot{
   
       if (!self.textView.text.length) {
          // [SSTool error:SSKJLanguage(@"请输入您的手机号")];
           [MBProgressHUD showError:@"请输入您的个性签名"];
           return NO;
       }
    if (![self urlsArray].count) {
        [MBProgressHUD showError:@"请上传图片"];
        return NO;
    }
    if (_dataList.count) {
        for (NSDictionary *dic in _dataList) {
            if ([_textView.text containsString:dic[@"content"]]) {
                [MBProgressHUD showError:@"您输入的内容包含敏感词汇请重新输入"];
                _textView.text = @"";
                return NO;
            }
        }
    }

    return YES;
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, TSScreenWidth, TSScreenHeight - Height_NavBar ) style:(UITableViewStyleGrouped)];
        
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = kWhiteColor;
        _tableView.tableHeaderView = self.headerView;
        if (@available(iOS 11.0, *))
        {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            _tableView.estimatedRowHeight = 0;
            _tableView.estimatedSectionHeaderHeight = 0;
            _tableView.estimatedSectionFooterHeight = 0;
        }
        else
        {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
       
    }
    return _tableView;
}
#pragma mark ----敏感词请求
-(void)requstgetsensitivewords{
    NSDictionary *pamas = @{};
    WS(weakSelf);
       [NetWorkTools postConJSONWithUrl:@"getsensitivewords" parameters:pamas success:^(id responseObject) {
           
           NSString *result = responseObject[@"result"];
           NSString *resultNote = responseObject[@"resultNote"];
           NSArray *dataList = responseObject[@"dataList"];
           if ([result integerValue] == 0) {
               weakSelf.dataList = dataList;
           }else{
               [MBProgressHUD showError:resultNote];
           }
           //
          } fail:^(NSError *error) {
              
    }];
}
@end
